<%@page import="weaver.conn.RecordSet"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="com.zlgs.cqyf.vote.ConnectionUtil"%>
<%@ page import="java.io.PrintWriter" %>
<%@ page import="weaver.hrm.User" %>
<%@ page import="weaver.hrm.HrmUserVarify" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%
    response.setCharacterEncoding("UTF-8");
    response.setContentType("application/json; charset=utf-8");
    JSONObject retObject = new JSONObject();
    retObject.put("code", 200);
    retObject.put("msg", "操作成功");
    try {
        // 获取用户信息
        String userId ="";
        String sql = "";
        String deptId = "";
        String name = "自测姓名";
        String dept = "自测部门";
        /* if (!ConnectionUtil.is_local){
            User user = HrmUserVarify.getUser (request , response) ;
            if (null == user)  throw new RuntimeException("请重新登录");
            userId = user.getLoginid();
            name = user.getUsername();
            // 留言人部门名称
            //deptId = user.getUserDepartment();
        } */
        Object requestType = request.getParameter("requestType");
		if(null != requestType && !"".equals(requestType.toString())&&"mobile".equals(requestType.toString())){
			Object userIdObj = request.getParameter("userId");
			if(null == userIdObj || "".equals(userIdObj.toString())){
				throw  new RuntimeException("请求用户Id为空!");
			}
			
			 userId = userIdObj.toString();
			 String userSQL = "select top 1 *    from HrmResource where   loginid = N'"+userId+"' ORDER BY ID DESC";
			 RecordSet  userSet= new RecordSet();
			 userSet.executeSql(userSQL);
			 if(userSet.next()){
				 name =  userSet.getString("lastname");
				 deptId = userSet.getString("departmentid");
			 }else{
				 throw  new RuntimeException("该用户不存在于OA!");
			 }
			 	sql = "select * from HrmDepartment where id='"+deptId+"' ";
		        RecordSet  set0 = new RecordSet();
		        set0.executeSql(sql);
		        if (set0.next()){
		        	retObject.put("deptId",deptId);
		            dept = set0.getString("departmentname");
		        }
		}else{
			if (!ConnectionUtil.is_local){
			    User user = HrmUserVarify.getUser (request , response) ;
	            if (null == user)  throw new RuntimeException("请重新登录");
	            userId = user.getLoginid();
	            name = user.getUsername();
	            // 留言人部门名称
	            deptId = String.valueOf(user.getUserDepartment());
	        	retObject.put("deptId",deptId);
	            sql = "select * from HrmDepartment where id="+deptId;
	            RecordSet  set = new RecordSet();
	            set.executeSql(sql);
	            while (set.next()){
	                dept = set.getString("departmentname");
	            }
			}
			
		 }

       
        // URL 参数
        Integer awards_participator_id  = Integer.parseInt(request.getParameter("awardsParticipatorId"));  // 名单Id
        String message = request.getParameter("message");                               // 留言
        String photo = "/portals/images/user_adient.png";                               // 留言人头像
        // 插入数据
        sql = "INSERT INTO vote_awards_message(" +
                    ("awards_participator_id, ") +
                    (name==null?"":"name, ") +
                    (photo==null?"":"photo, ") +
                    (dept==null?"":"dept, ") +
                    (message==null?"":"message, ") +
                    "create_time) " +
                "values(" +
                    (awards_participator_id+", ") +
                    (name==null?"":"N'"+name+"', ") +
                    (photo==null?"":"N'"+photo+"', ") +
                    (dept==null?"":"N'"+dept+"', ") +
                    (message==null?"":"N'"+message+"', ") +
                    "GETDATE())";
        RecordSet set  = new RecordSet();
	  	set.executeSql(sql);
        // 更新提名留言数量
        sql = "update vote_awards_participator set message_num =(message_num+1)  where id="+awards_participator_id;
        RecordSet set1  = new RecordSet();
	  	set1.executeSql(sql);
    } catch (Exception e) {
        e.printStackTrace();
        retObject.put("code", -1);
        retObject.put("msg", e.getMessage());
    } finally {
        PrintWriter printWriter = response.getWriter();
        printWriter.append(retObject.toJSONString());
        if (printWriter != null) printWriter.close();
    }
%> 